<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>计算</title>
    <style>
      label {
        display: block;
        padding: 6px;
      }
    </style>
    <script>
      var userAnswer = [];
      var answer = [];
      var dadui = 0;
      function dati() {
        innerHTML = math1 + "+" + math2 + "=";
        for (var i = 1; i < 11; i++) {
          var math1 = Math.floor(Math.random() * 10);
          var math2 = Math.floor(Math.random() * 10);
          answer.push(math1 + math2);

          document.getElementById("dati" + i).innerHTML =
            math1 + "+" + math2 + "=";
          document.getElementById("tiankong" + i).type = "number";
        }
      }

      function tijiao() {
        for (var a = 1; a < 11; a++) {
          userAnswer.push(document.getElementById("tiankong" + a).value);
        }
        for (var b = 0; b < 10; b++) {
          if (userAnswer[b] == answer[b]) {
            dadui++;
          }
        }
        var name = document.getElementById("name").value;
        var point = dadui * 10;
        var cookie = document.cookie;

        //------------------------------------------------------------------------------------------------以下是cookie

        var maxdefen = 0;
        if (cookie) {
          var cindexof = cookie.indexOf("user=");
          var csubstring = cookie.substring(cindexof + 5);
          var start = cookie.substring(cindexof + 8);
          var end = start.indexOf("姓名");
          console.log(end, 666, start, 777, csubstring);
          maxdefen = start.substring(0, end);
          var maxname = start.substring(end);
          console.log(maxname);
        }

        if (point >= maxdefen) {
          document.cookie = "user=" + "得分:" + point + "姓名:" + name;
          document.write(
            "满分:100分;   原来的最高分：" +
              maxdefen +
              maxname +
              "   你的得分：" +
              point +
              "分;   姓名：" +
              name +
              "恭喜你！成为了最高分！"
          );
        } else {
          document.write(
            "满分:100分;   原来的最高分：" +
              maxdefen +
              maxname +
              "   你的得分：" +
              point +
              "分;   姓名：" +
              name
          );
        }
      }
    </script>
  </head>
  <body>

    <script>
      var htmlstr = "";
      for (var i = 1; i < 11; i++) {
        htmlstr += `
    <label>
      <span id="dati${i}"></span>
      <input
        type="hidden"
        autocomplete="off"
        id="tiankong${i}"
        size="1"
        maxlength="2"
      />
    </label>
    `;
      }

      document.write(htmlstr);

      dati();
    </script>

    <div id="jisuan"></div>

    <input type="button" value="提交表单" onclick="tijiao()" />
  </body>
</html>
